home *** CD-ROM | disk | FTP | other *** search
- Path: nntp0.brunel.ac.uk!usenet
- From: cspgrgr@brunel.ac.uk (R Ghosh-Roy)
- Newsgroups: comp.lang.c
- Subject: sizeof + offsetof for browsing structures
- Date: 11 Apr 1996 16:52:38 GMT
- Organization: Brunel University, Uxbridge, UK
- Distribution: world
- Message-ID: <4kjdcm$e4l@mimas.brunel.ac.uk>
- Reply-To: cspgrgr@brunel.ac.uk
- NNTP-Posting-Host: molnir.brunel.ac.uk
- Keywords: sizeof, offsetof, structure, schema
-
-
-
- Hello Everybody,
-
- I am a bit confused about "pointer arithmatic" and therefore would appreciate
- your expert help. The following is the problem:
-
- I am defining a schema (or templates) using the following structure
- definitions:
-
- typedef struct SchemaStruc
- {
- char *name ;
-
- List entities ;
- } ;
-
- typedef struct EntityStruc
- {
- int id ;
- char *name, description ;
- long int strucsize ;
-
- List attributes ;
- } ;
-
- typedef struct AttributeStruc
- {
- int id ;
- char *name, description ;
- long int fieldoffset ;
- } ;
-
-
- / /
- schema --------|-- entity --------|-- attribute
- \ \
- 1-many 1-many
-
- I have defined three entities A,B and C. For example, entity A defines
- a house and one creates lots of instances of A stored in an array. The
- instances of B (people) and C (address) are also stored in arrays.
-
- |--|
- | |-->A1->A2->A3->A4->etc (instances of entity A)
- | |
- | |-->B1->B2->B3->B4->B5->B6->B7->etc (instances of entity B)
- | |
- | |-->C1->C2->C3->C4->C5->etc (instances of entity C)
- |--|
-
-
- Each entity (say of size X using "sizeof") has many attributes and each
- attribute comes with an offset length (say Y using "offsetof") within the
- entity. I can then search for any attribute instance within an array of
- entity instances just by doing pointer arithmatic:
-
- entity Z
- - |--------| -
- | | | |
- | | | Y offset
- | | | |
- size X | | |
- | | Za | -
- | | |
- - |--------|
-
-
- /** Get X (sizeof) from the chosen entity definition **/
- /** Get Y (offsetof) from the chosen entity-attribute definition **/
-
- for (x=start_of_array;
- x < number_of_instances * X;
- x+=X)
- {
- x+Y points directly to the attribute value I am after.
-
-
- }
-
- This avoids me writing accessing function for each attribute. The program
- looks at the entity structure size and the field offset at the meta level
- and then uses pointer arithmatic to traverse the entity instances and its
- attribute instances. The beauty is that as its the same for all attributes,
- I dont need individual function for accessing each attribute definition.
-
-
- However, I now wish to add ANOTHER attribute which points to other entities.
-
- entity A
- |--------|
- . . . | | . . . . . . . . . . . . . . . .
- | |
- | |
- | | entity B
- | Ab ---|------->|--------|
- | | | |
- |--------| | |
- . . . . . . . . . . . . | | . . . . . . . . . . . . . . . .
- | |
- | | entity C
- | Bc ---|-------->|----------|
- | | | |
- | | | |
- | | | |
- |--------| | |
- | |
- | Cd |
- | |
- . . . . . . . . . . . . . . . . . . . . . | | . . . . . . . . . .
- | |
- | |
- |----------|
-
- WARNING: The solution to the following question is NOT trivial.
- My question is: can I use sizeof and offsetof to get to Cd as I
- can do to get to Ab (described above)? Ie, *NOT* write specific
- functions for each attribute (Cd) to access its value by traversing
- down from entity A, but use only sizeof and offsetof.
-
- Thanks,
-
- Rana
-
-
- --
- R. Ghosh-Roy, Research Fellow @ BIPS -- R.Ghosh-Roy@brunel.ac.uk -- Extension 2772
-
-
-
-
-